import _ from "lodash";
import Vue from "vue";
import {MOUDLE_MAP} from "@constants"


import { getStringSet } from "./dataStruct";

export const addProps = (props, Component) => {
    return Vue.extend({
        name: Component.name,
        functional: true,
        render: function(h) {
            return <Component {...{ props }} />;
        },
    });
};

export const queryParmKeyImport = (key, Component) => {
    return Vue.extend({
        name: Component.name,
        functional: true,
        render: function(h) {
            let keylist = [];

            let currentRouteQuery = _.get(
                window,
                "g_instance.$route.query",
                {}
            );

            if (typeof key === "string") {
                keylist.push(currentRouteQuery[key]);
            } else if (typeof key === "object") {
                for (let i in key) {
                    keylist.push(currentRouteQuery[key[i]]);
                }
            }

            return <Component key={keylist.join("")} />;
        },
    });
};

export const getRouteItemByMenuItem = (menu) => {
    let itemSet = getStringSet(menu.description, "--");
    let isIframe = !itemSet.frontName;

    let queryParams = {
        path: menu.path,
    }
    if(menu.moduleName === MOUDLE_MAP.cms) {
        queryParams.moduleName = menu.moduleName;
        queryParams.iframeUrl = menu.iframeUrl;
    }
    if (isIframe) {
        return {
            name: "iframe",
            query: queryParams,
        };
    }

    return {
        name: itemSet.frontName,
    };
};
